<?php

namespace app\modules\admin\controllers;

use app\common\services\StaticServices;
use app\models\User;
use Yii;
use yii\base\Exception;
use yii\web\Controller;
use yii\web\Response;

class UserController extends Controller {
  public function actionIndex()
  {
    $p = Yii::$app->request->get('p', 1);
    $user_info = User::find()->where(['user_status' => 1])->andWhere(['in', 'user_role', [1, 2, 3, 4]])->andWhere(['<>', 'user_ID', '0']);

    $page_size = StaticServices::$page;
    $total_count = $user_info->count();
    $total_page = ceil($total_count / $page_size);

    $list = $user_info->offset(($p - 1) * $page_size)->orderBy(['create_time' => SORT_DESC])->limit($page_size)->all();

    return $this->render('index', ['list' => $list, 'p' => $p, 'total_page' => $total_page]);
  }

  public function actionSet()
  {
    $user_id = Yii::$app->request->get('id');
    $user_info = [];
    if ($user_id) {
      $user_info = User::find()->where(['id' => $user_id, 'user_status' => 1])->one();
    }

    $userRole = StaticServices::getUserRole();
    if (Yii::$app->admin->identity->id == 1 && empty($user_info)) {
      unset($userRole[4]);
    }

    if (Yii::$app->request->isAjax) {
      Yii::$app->response->format = Response::FORMAT_JSON;
      try {
        $user_name = trim(Yii::$app->request->post('user_name', ''));
        $user_role = intval(Yii::$app->request->post('user_role'));
        $user_ID = trim(Yii::$app->request->post('user_ID'));
        $role_pid = intval(Yii::$app->request->post('role_pid'));
        $user_phone = trim(Yii::$app->request->post('user_phone'));
        $user_id = Yii::$app->request->post('user_id');

        if (mb_strlen($user_name, 'utf-8') < 1) {
          throw new Exception('人员名称不能为空');
        }

        if (!StaticServices::is_idcard($user_ID)) {
          throw new Exception('请填写正确的身份号码');
        }

        if (!preg_match("/^1[345678]{1}\d{9}$/", $user_phone)) {
          throw new Exception('请填写正确的电话号码');
        }
        if (mb_strlen($user_role, 'utf-8') < 1) {
          throw new Exception('人员身份不能空');
        }
        $user_arr = User::find()->where(['id' => $user_id])->one();
        if ($user_arr) {
          $user_info = $user_arr;
          $user_info->user_name = $user_name;
          $user_info->user_phone = $user_phone;
          $user_info->user_ID = $user_ID;
          $user_info->user_role = $user_role;
          $user_info->role_pid = 0;
          $user_info->update_time = time();
          $user_info->save(0);
          return ['code' => 200, 'msg' => '操作成功'];
        } else {
          $default_pwd = strtolower(StaticServices::getRandChar(6));
          if (User::getCard($user_ID)) {
            throw new Exception('此身份证已经存在');
          }
          $user_info = new User();
          $user_info->create_time = time();
          $user_info->user_name = $user_name;
          $user_info->user_ID = $user_ID;
          $user_info->user_pwd = $default_pwd;
          $user_info->user_phone = $user_phone;
          if ($user_role == 4) {
            $user_info->role_pid = $role_pid;
          } else {
            $user_info->role_pid = 0;
          }
          $user_info->user_role = $user_role;
          $user_info->update_time = time();
          if ($user_info->save(0)) {
            //发送短信
            $template = "欢迎使用全民推荐平台。登录账号为您的手机号，初始密码为{code}，密码可在设置页面修改；如非本人操作，请忽略此短信。";
            StaticServices::getCode($user_info->user_pwd, $template, $user_info->user_phone, 2);
          }
          return ['code' => 200, 'msg' => '操作成功'];
        }

      } catch (Exception $e) {
        return ['code' => -200, 'msg' => $e->getMessage()];
      }
    }

    return $this->render('set', ['info' => $user_info, 'userRole' => $userRole]);
  }

  public function actionOps()
  {
    if (Yii::$app->request->isAjax) {
      Yii::$app->response->format = Response::FORMAT_JSON;
      try {
        $user_id = intval(Yii::$app->request->post('user_id'));
        if (!$user_id) {
          throw new Exception('参数错误');
        }
        $user_info = User::find()->where(['id' => $user_id])->one();
        if (!$user_info) {
          throw new Exception('数据有误');
        }
        $user_info->user_status = 0;
        $user_info->update_time = time();
        $user_info->save(0);
        return ['code' => 200, 'msg' => '操作成功'];
      } catch (Exception $e) {
        return ['code' => -200, 'msg' => $e->getMessage()];
      }
    }
  }
}
